package com.labor_management_system.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.labor_management_system.Entity.LaborEngineering;
import com.labor_management_system.Entity.WorkDetails;
import com.labor_management_system.Service.EnterprisesService;
import com.labor_management_system.Service.LaborEngineeringService;
import com.labor_management_system.Service.ProjectService;
import com.labor_management_system.Service.WorkDetailsService;
import com.labor_management_system.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class LaborEngineeringController {
    //劳务工程管理
    //劳务工程管理

    @Autowired
    private LaborEngineeringService laborEngineeringService;

    @Autowired
    private EnterprisesService enterprisesService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private WorkDetailsService workDetailsService;

    //显示所有工程，分页
    @PostMapping("ShowAllEngineer")
    public R ShowAllEngineer(@RequestParam(value = "selectPage",defaultValue = "1") Integer selectPage, @RequestParam(value = "PageSize",defaultValue = "100") Integer PageSize){
        return R.ok().data("EngineerInfoList",laborEngineeringService.showAllengineer(selectPage,PageSize));
    }

    //条件查询，参与单位，项目名称，申报日期
    @PostMapping("SelectByLaborEngineering")
    public R SelectByLaborEngineering(@RequestBody LaborEngineering laborEngineering){
        return R.ok().data("EngineerInfoList",laborEngineeringService.selectBy(laborEngineering));
    }

    //显示工程详细信息
    //前端负责，已经传入到tabledata里面了，根据项目单位名称查找到相应的数组即可
    //只负责查找工程明细，通过传来的laborEngineeringId
    @PostMapping("selectWorkDetailsBylaborEngineeringId")
    public R selectWorkDetailsBylaborEngineeringId(@RequestParam("laborEngineeringId") String laborEngineeringId){

        return R.ok().data("workDetailsList",workDetailsService.selectListByEngineerId(laborEngineeringId));
    }


    //新建工程
    @PostMapping("AddEngineer")
    public R AddEngineer(@RequestParam("enterprisesId") String enterpriseId,
                         @RequestParam("projectId") String projectId,
                         @RequestParam("filingCycle1") Long filingCycle1,
                         @RequestParam("filingCycle2") Long filingCycle2,
                         @RequestParam("workDetails") String workDetailsJson,
                         @RequestParam("enterprisesName") String enterprisesName,
                         @RequestParam("projectName") String projectName,
                         @RequestParam(value = "multipartFile") MultipartFile multipartFile) throws IOException {


        LaborEngineering laborEngineering = new LaborEngineering();

        laborEngineering.setEnterprisesId(enterpriseId);
        laborEngineering.setProjectId(projectId);
        laborEngineering.setFilingCycle1(filingCycle1);
        laborEngineering.setFilingCycle2(filingCycle2);
        laborEngineering.setEnterprisesName(enterprisesName);
        laborEngineering.setProjectName(projectName);

        ObjectMapper objectMapper = new ObjectMapper();

        List<WorkDetails> workDetailsList = objectMapper.readValue(workDetailsJson, new TypeReference<List<WorkDetails>>() {});

        System.out.println("workDetailsList = " + workDetailsList);

        //EngineerId先放入每个实体类中
        String Id = UUID.randomUUID().toString().replace("-", "");

        laborEngineering.setLaborEngineeringId(Id);

        for (int i = 0; i < workDetailsList.size(); i++) {
            workDetailsList.get(i).setLaborEngineeringId(Id);
        }

        return laborEngineeringService.insertEnginer(laborEngineering,multipartFile) &&
                workDetailsService.inserBatchList(workDetailsList)? R.ok():R.error().message("新增失败");

    }

    //单位信息选择器
    @PostMapping("Unitselect")
    public R Unitselect(){
        return R.ok().data("UnitList",enterprisesService.selectAllUnit());
    }

    //项目信息选择器
    @PostMapping("Projectselect")
    public R Projectselect(){
        return R.ok().data("ProjectList",projectService.selectAllProject());
    }

    //编辑工程
    @PostMapping("EditEngineer")
    public R EditProject(@RequestParam("Engineer") String EngineerJson,
                         @RequestParam("WorkDetails") String WorkDetailsJson,
                         @RequestParam(value = "multipartFile",required = false)MultipartFile multipartFile
    ) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        LaborEngineering laborEngineering = objectMapper.readValue(EngineerJson,LaborEngineering.class);

        System.out.println("laborEngineering = " + laborEngineering);

        List<WorkDetails> workDetailsList = objectMapper.readValue(WorkDetailsJson, new TypeReference<List<WorkDetails>>() {});

        System.out.println("workDetailsList = " + workDetailsList);

        String laborEngineeringId = laborEngineering.getLaborEngineeringId();

        return laborEngineeringService.updateEngineer(laborEngineering,laborEngineeringId,multipartFile)
                && workDetailsService.updateBatchList(workDetailsList,laborEngineeringId)?
                R.ok():R.error().message("新增失败");
    }

    //删除工程，附带删除工程下的工程明细
    @PostMapping("DeleteEngineer")
    public R DeleteProject(@RequestParam("laborEngineeringId") String laborEngineeringId){

//        laborEngineeringService.deleteEngineer(laborEngineeringId);
        QueryWrapper<WorkDetails> listQueryWrapper = new QueryWrapper<>();
        listQueryWrapper.eq("labor_engineering_id",laborEngineeringId);
        workDetailsService.getBaseMapper().delete(listQueryWrapper);

        //删除父表
        QueryWrapper<LaborEngineering> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("labor_engineering_id",laborEngineeringId);
        laborEngineeringService.getBaseMapper().delete(queryWrapper);
        return R.ok();
    }

    //删除工程明细
    @PostMapping("DeleteWorkDetails")
    public R DeleteProjectList(@RequestParam("workDetailsId") String workDetailsId){

        workDetailsService.deleteworkDetails(workDetailsId);

        return R.ok();
    }
}
